<script>
import { getDepartment, getManagerList, addDepartment, getDepartmentDetail, updateDepartment } from '@/api/department'

export default {
  name: 'AddDepts',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    pid: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      managerList: [], // 部门负责人列表
      formData: {
        code: '', // 部门编码
        introduce: '', // 部门介绍
        managerId: '', // 部门负责人
        name: '', // 部门名称
        pid: '' // 父级部门id
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称的长度为2-10个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              let departments = await getDepartment()
              if (this.formData.id) {
                departments = departments.filter(department => department.id !== this.formData.id)
              }
              if (departments.some(department => department.name === value)) {
                callback(new Error('部门中已经有该名称了'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门编码的长度为2-10个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              let departments = await getDepartment()
              if (this.formData.id) {
                departments = departments.filter(department => department.id !== this.formData.id)
              }
              if (departments.some(department => department.code === value)) {
                callback(new Error('部门中已经有该编码了'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        managerId: [
          { required: true, message: '部门负责人不能为空' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '部门介绍的长度为1-100个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    deptTitle() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    // 关闭弹窗 这样写可以清空表单中没有的id等元素
    close() {
      this.formData = {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      }
      this.$refs.deptsRef.resetFields()
      this.$emit('update:showDialog', false)
    },

    // 获取负责人列表
    async getManagerList() {
      this.managerList = await getManagerList()
    },

    // 添加/修改部门
    addDepartment() {
      this.$refs.deptsRef.validate(async valid => {
        if (valid) {
          if (this.formData.id) {
            // 修改
            await updateDepartment(this.formData)
            this.$message.success('修改成功')
          } else {
            // 新增
            this.formData.pid = this.pid
            await addDepartment(this.formData)
            this.$message.success('添加成功')
          }

          // 通知父组件刷新页面
          this.$emit('flush')
          // 关闭弹窗
          this.close()
        }
      })
    },

    // 获取部门详情
    async getDepartmentDetail() {
      this.formData = await getDepartmentDetail(this.pid)
    }
  }
}
</script>

<template>
  <el-dialog :title="deptTitle" :visible="showDialog" width="50%" @close="close">
    <el-form ref="deptsRef" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" size="mini" style="width: 80%" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" size="mini" style="width: 80%" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="formData.managerId" size="mini" style="width: 80%" placeholder="请选择负责人">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" type="textarea" :rows="4" size="mini" style="width: 80%" placeholder="1-100个字符" />
      </el-form-item>
      <el-form-item>
        <el-row style="width: 80%">
          <el-col style="text-align: center">
            <el-button type="primary" size="mini" @click="addDepartment">确 定</el-button>
            <el-button size="mini" @click="close">取 消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
